import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router';
import Dashboard from '@/views/Dashboard.vue';

// 定义路由记录
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'dashboard',
    component: Dashboard,
    meta: {
      title: '控制台',
      icon: 'Server'
    }
  },
  {
    path: '/servers',
    name: 'servers',
    component: () => import('@/views/ServerList.vue'),
    meta: {
      title: '服务器管理',
      icon: 'Database'
    }
  },
  {
    path: '/server/:id',
    name: 'server-detail',
    component: () => import('@/views/ServerDetail.vue'),
    meta: {
      title: '服务器详情',
      icon: 'Server'
    },
    props: true
  },
  {
    path: '/users',
    name: 'users',
    component: () => import('@/views/UserList.vue'),
    meta: {
      title: '用户管理',
      icon: 'User'
    }
  },
  {
    path: '/settings',
    name: 'settings',
    component: () => import('@/views/Settings.vue'),
    meta: {
      title: '系统设置',
      icon: 'Setting'
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login.vue'),
    meta: {
      title: '登录',
      icon: 'Login'
    },
    hidden: true
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'not-found',
    component: () => import('@/views/NotFound.vue'),
    meta: {
      title: '页面未找到',
      icon: 'Warning'
    },
    hidden: true
  }
];

// 创建路由实例
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition) {
    // 始终滚动到顶部
    return { top: 0 };
  }
});

// 路由守卫 - 设置页面标题
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = `${to.meta.title} - 幻兽帕鲁开服管理器`;
  }
  next();
});

export default router;